package com.logger.layouts;

import com.logger.LogMessage;
import com.logger.common.Utils;

import java.text.SimpleDateFormat;

/**
 * 提供更加丰富的信息
 * Created by chenxunlin01 on 2016/1/17.
 */

public class RichLayout extends ALayout {

    private String dateformat = "yyyy-MM-dd HH:mm:ss";

    @Override
    public String format(LogMessage message) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(sdf.format(message.getDate()));
        stringBuilder.append(" [");
        stringBuilder.append(message.getThreadName());
        stringBuilder.append("] ");
        if (message.getThrowableInfo() != null && message.getThrowableInfo().getEl() != null) {
            String eleStr = message.getThrowableInfo().getEl().toString();
            int start = eleStr.indexOf("(");
            stringBuilder.append(eleStr.substring(start));
            stringBuilder.append(Utils.LINE_RETURN);
        }
        stringBuilder.append(message.getMessage());
        stringBuilder.append(Utils.LINE_RETURN);
        return stringBuilder.toString();
    }
}
